from u import Sess, S
from pathlib import Path
import json

# --------------------------------------------------

# index the files
# 1. read the XY-index.json
p0 = Path.cwd() / "XY"
p1 = p0 / "XY-index.json"
idx = json.loads(p1.read_text())


async def get_summary(n: str) -> str:
    k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
    s = Sess.new(model="mistralai/codestral-2501", url=k['url'], key=k['key'])
    s.add_q(
        f"Hi, I want to archive my unit-test scritpt, here's the test description:\n\n:```markdown\n{c}```. Can you give me a one-line summary of this test ?\n"
    ); await s.ask_for_a(); None
    return s.ms[-1].content

# idx[n] = {"summary": o,
#           "author": author,
#           "date": datetime.datetime.now().isoformat(),
#           }
# n = '01-01'
# c = (p0 / f'{n}.X.md').read_text()

# --------------------------------------------------

from u import Sess, S
from pathlib import Path
import json
import datetime

# --------------------------------------------------

# index the files
# 1. read the XY-index.json
p0 = Path.cwd() / "XY"
p1 = p0 / "XY-index.json"
idx = json.loads(p1.read_text())


async def get_summary(n: str) -> str:
    k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
    s = Sess.new(model="mistralai/codestral-2501", url=k['url'], key=k['key'])
    s.add_q(
        f"Hi, I want to archive my unit-test scritpt, here's the test description:\n\n:```markdown\n{c}```. Can you give me a one-line summary of this test ?\n"
    ); await s.ask_for_a(); None
    return s.ms[-1].content

# 2. get *.X.md
l = p0.glob("*.X.md")
# for each unindex file
for p in sorted(l):
    n = p.name.removesuffix(".X.md")
    if n not in idx:
        print(f"Processing {S.BLUE} {n} {S.NOR}")
        # index and write
        idx[n] = {
            "summary": await get_summary(n),
            "date": datetime.datetime.now().isoformat(),
        }
        if author:
            idx[n]["author"] = author

# 2. write the XY-index.json
p1.write_text(json.dumps(idx, indent=2))
